<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=big5">
        <title>Check AD for PHP</title>
    </head>
    <body>
        <?php

            $account = "T0255@hhsh.chc.edu.tw";//$_POST['account'];
            $password = "hhsh1264";//$_POST['password']; //��X

            $dn="cn=Users,dc=hhsh,dc=chc,dc=edu,dc=tw";
            $inforequired = array("displayName","memberOf");
            $filter="(|(cn=T0255))";

            $server = "ldap://srv-ad.hhsh.chc.edu.tw";
            $conn = ldap_connect($server) or die("Could not connect to LDAP server");

            //ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3) or die (" Could not set protocol version.<br>\n\n");
            //ldap_set_option($conn, LDAP_OPT_REFERRALS,0) or die (" Could not set option referrals.<br>\n\n");

            $bind=ldap_bind($conn, $account, $password);

            if ($bind) {
                echo "passed!";
            } else {
                echo "failed!";
            }

            $user_result = ldap_search($conn,$dn,$filter,$inforequired);
            $user_info = ldap_get_entries($conn,$user_result);

            //echo $user_info[0]['displayname'][0];
            echo "<br/>";

            //print_r($user_info[0]['memberof']);
            for($i=0;$i<$user_info[0]['memberof']["count"];$i++){
                $val=explode(",", $user_info[0]['memberof'][$i]);
                $group_list = $group_list . str_replace("CN=", "", $val[0])  . ",";
            }
            
//            foreach ($user_info[0]['memberof'] as $group){
//                $value = explode(",", $group);
//                //echo strpos($value[0], "CN=");
//                if(strpos($value[0], "CN=")==0){
//                    $group_list = $group_list . substr($value[0],3) . ",";
//                }
//
//            }

            echo $group_list;
            //print_r($user_info[0]['memberof']);
            /*for ( $i=1; $i<$max; $i+=1) {
                echo "user name==>";
                echo $user_info[$i-1]['displayname'][0];
                echo "<br/>";
            }*/

            ldap_unbind($conn);
        ?>
    </body>
</html>
